function timestampToTime(timestamp) {
        var date = new Date(timestamp * 1000);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
        var Y = date.getFullYear() + '-';
        var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
        var D = date.getDate() + ' ';
        var h = date.getHours() + ':';
        var m = date.getMinutes() + ':';
        var s = date.getSeconds();
        return Y+M+D+h+m+s;
    }


layui.use(['form','layer','table','laytpl'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laytpl = layui.laytpl,
        table = layui.table;
		
	const tableVue=new Vue({
			el: '#author-book-list',
			data:{
				  all_data:null,
				  tableIns:null,
					user:null,
					categorys:{},
					novel_count:0
			},
			methods:{
				tablefill:function(){
					this.tableIns = table.render({
						elem: '#bookList',
						cellMinWidth : 95,
						page : true,
						height : "full-125",
						id : "bookListTable",
						cols : [[
							{type: "checkbox", fixed:"left", width:50},
							{field: 'novelId', title: 'ID', width:60, align:"center"},
							{field: 'author', title: '作者', align:"center",templet:function(d){
								return  d.author==null?"不存在": d.author.penName
							}},
							{field: 'novelName', title: '书名',align:'center'},
							{field: 'type', title: '类型',align:'center',templet:function(d){
								var types=""
								for(var i=0;i<d.categories.length;i++){
									types+=d.categories[i].categoryName+","
								}
								
								
								return types.substring(0,types.length-1)
							}},
							{field: 'cover', title: '封面地址', align:'center'},
							{field: 'collectionNumber', title: '收藏数', align:'center', width:60},
							{field: 'sentiment', title: '人气', align:'center', width:60},
							{field: 'introduce', title: '简介', width:350, align:'center'},
							{field: 'novelSource', title: '源',  align:'center'},
							{title: '操作', width:170, templet:'#bookListBar',fixed:"right",align:"center"}
						]],data:this.all_data
					});
				},
				findByName:function(id){
					Vue.http.post(global_url+'/admin/novel/findAuthor/'+id).then(
						response=>{
							this.all_data=response.body.datas
							this.tablefill()
						},
						response=>{
						}
					)
				},
				getAuthorNovel:function(id){
						var url=global_url+'/admin/author/novelCount'
						this.$http.get(url+'/'+id)
							.then(
							response=>{
								this.novel_count=response.body.datas
								$(".novle-conunt").val(this.novel_count)
							},					
							response=>{
							})
				},
				getAuthorUser:function(id){
					var url=global_url+'/admin/author/findByUser'
				
					this.$http.get(url+'/'+id)
						.then(
						response=>{
							this.user=response.body.datas
							if(this.user!=null){
								$(".user-name").val(this.user.account)
							}
						},					
						response=>{
						})
				
				},
				deletNovel:function(id){
					Vue.http.post(global_url+'/admin/novel/delete/'+id).then(
						response=>{
							//刷新父页面
							layer.msg(response.body.msg)
							this.findByName($(".id").val())

						},
						response=>{
							layer.msg(response.body.msg)
						}
					)
				},
				addcategorysHtml:function(data){
						var newHtml=''
						for(var i=0;i<this.categorys.length;i++){
							var html='<input type="checkbox" name='+this.categorys[i].categoryName+' lay-skin="primary" title='+this.categorys[i].categoryName+'>'
							
							if(data!=null){
								data.forEach(function(value,j){
									if(bookVue.categorys[i].categoryName==value){
										html='<input type="checkbox" name='+bookVue.categorys[i].categoryName+' lay-skin="primary"  checked  title='+bookVue.categorys[i].categoryName+'>'
									}
								})
							}
							newHtml+=html
						}
						return newHtml;
				},
				PenName:function(name){
					data={
						"id":id,
						"penName":name
					}
					this.$http.post(global_url+'/admin/author/updata/',data)
						.then(
						response=>{
							layer.msg(response.body.datas)
						},					
						response=>{
							layer.msg(response.body.status)
						})
				}
				
			}
		});
		
		
		
		setTimeout(function(){
				id=$(".id").val()
				//获取用户信息
				tableVue.getAuthorUser(id)
				//获取小说数
				tableVue.getAuthorNovel(id)
				//获取小说列表
				tableVue.findByName(id)
				
		},500)
		
		
		
		
  $(".user-page").on("click",function(){
			if(tableVue.user==null){
				layer.msg('外站作者没有用户信息')
				return
			}
			Vue.http.post(global_url+'/admin/rolePermissions/').then(
					response=>{
						var  permissions=response.body.datas;
						var edit=tableVue.user;
						var index = layui.layer.open({
								title : "添加用户",
								type : 2,
								content : "../user/userAdd.html",
								success : function(layero, index){
										var body = layui.layer.getChildFrame('body', index);
										if(edit){
												body.find(".userName").val(edit.account);  //登录名
												body.find(".userEmail").val(edit.email);  //邮箱
												body.find(".userSex input[value="+edit.gender+"]").prop("checked","checked");  //性别
												body.find(".userStatus").val(edit.type);    //用户状态
												body.find(".userDesc").text(edit.autograph);    //用户简介
												body.find(".userId").text(edit.userId)  //用户id
									
										}
										for(var i = 0 ; i< permissions.length;i++){
											body.find(".userGrade").append('<option value="'+permissions[i].name+'" >'+permissions[i].name+'</option>')
										}
										form.render();
	
										setTimeout(function(){
												layui.layer.tips('点击此处返回作者编辑页面', '.layui-layer-setwin .layui-layer-close', {
														tips: 3
												});
										},500)
								}
						})
						layui.layer.full(index);
						window.sessionStorage.setItem("index",index);
						//改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
						$(window).on("resize",function(){
								layui.layer.full(window.sessionStorage.getItem("index"));
						})
					},
					response=>{
						
					}
				)
		
		
	});

   
	
	//提交按钮
	form.on("submit(changeAuthor)",function(data){
		tableVue.PenName(data.field.name)
		return false
		
		
	});

	
	
	//小说管理
	function addNews(edit){
		Vue.http.post(global_url+'/admin/novel/categorys/all').then(
				response=>{
					
					var  category=response.body.datas.category;
					
					var index = layui.layer.open({
							title : "小说管理",
							type : 2,
							content : "../news/bookinfo.html",
							success : function(layero, index){
									var body = layui.layer.getChildFrame('body', index);
									if(edit){
											console.log(edit)	
											tableVue.categorys=category;
											var names=[]
											for(var p=0;p<edit.categories.length;p++){
												names[p]=edit.categories[p].categoryName
											}
											var newHtml=tableVue.addcategorysHtml(names);
										
											body.find(".id").val(edit.id);
											body.find(".novelId").val(edit.novelId);
											body.find(".bookName").val(edit.novelName);
											body.find(".author").val(edit.author.penName);
											body.find(".collectionNumber").val(edit.collectionNumber);
											body.find(".sentiment").val(edit.sentiment);
											body.find(".introduce").val(edit.introduce);
											body.find(".cover").val(edit.cover);
											body.find(".thumbImg").attr("src",edit.cover);
											body.find(".pcheckbox").append(newHtml);
										
											form.render(null,"bookinfopage");
									}
									setTimeout(function(){
											layui.layer.tips('点击此处返回文章列表', '.layui-layer-setwin .layui-layer-close', {
													tips: 3
											});
									},500)
							}
					})
					
			
					layui.layer.full(index);
					//改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
					$(window).on("resize",function(){
							layui.layer.full(index);
					})
				},					
				response=>{
					layer.msg(response.body.status)
				})
		
	}
	
		//列表操作
		table.on('tool(bookList)', function(obj){
				var layEvent = obj.event,
						data = obj.data;

				if(layEvent === 'edit'){ //编辑
						addNews(data);
				} else if(layEvent === 'del'){ //删除
						layer.confirm('确定删除改小说？',{icon:3, title:'提示信息'},function(index){
				
							tableVue.deletNovel(data.id)
							layer.close(index);
					

						});
				} else if(layEvent === 'look'){ //预览
						layer.alert("此功能需要前台展示，实际开发中传入对应的必要参数进行文章内容页面访问")
				}
		});

})
